package com.buaa.divinationmaster.core.algorithm;

import java.util.HashMap;
import java.util.Map;

/**
 * 算法配置类
 * 用于管理各种占卜算法的配置参数
 */
public class AlgorithmConfig {
    
    private Map<String, Object> settings;
    
    public AlgorithmConfig() {
        this.settings = new HashMap<>();
        initializeDefaultSettings();
    }
    
    /**
     * 初始化默认设置
     */
    private void initializeDefaultSettings() {
        // 随机算法设置
        settings.put("useSecureRandom", true);
        settings.put("randomSeedBaseTime", true);
        
        // 性能设置
        settings.put("cacheEnabled", true);
        settings.put("maxCacheSize", 100);
        
        // 调试设置
        settings.put("debugMode", false);
        settings.put("logLevel", "INFO");
        
        // 算法特定设置
        settings.put("tarotShuffleIterations", 7);
        settings.put("hexagramCalculationMethod", "traditional");
        settings.put("coinFlipPhysicsSimulation", false);
    }
    
    // 基础设置方法
    public void setSetting(String key, Object value) {
        settings.put(key, value);
    }
    
    public Object getSetting(String key) {
        return settings.get(key);
    }
    
    public Object getSetting(String key, Object defaultValue) {
        return settings.getOrDefault(key, defaultValue);
    }
    
    public boolean hasSetting(String key) {
        return settings.containsKey(key);
    }
    
    public void removeSetting(String key) {
        settings.remove(key);
    }
    
    // 类型安全的设置获取方法
    public boolean getBooleanSetting(String key, boolean defaultValue) {
        Object value = settings.get(key);
        return value instanceof Boolean ? (Boolean) value : defaultValue;
    }
    
    public int getIntSetting(String key, int defaultValue) {
        Object value = settings.get(key);
        if (value instanceof Integer) {
            return (Integer) value;
        } else if (value instanceof Number) {
            return ((Number) value).intValue();
        }
        return defaultValue;
    }
    
    public String getStringSetting(String key, String defaultValue) {
        Object value = settings.get(key);
        return value instanceof String ? (String) value : defaultValue;
    }
    
    // 常用配置的便捷方法
    public boolean isSecureRandomEnabled() {
        return getBooleanSetting("useSecureRandom", true);
    }
    
    public void setSecureRandomEnabled(boolean enabled) {
        setSetting("useSecureRandom", enabled);
    }
    
    public boolean isCacheEnabled() {
        return getBooleanSetting("cacheEnabled", true);
    }
    
    public void setCacheEnabled(boolean enabled) {
        setSetting("cacheEnabled", enabled);
    }
    
    public int getMaxCacheSize() {
        return getIntSetting("maxCacheSize", 100);
    }
    
    public void setMaxCacheSize(int size) {
        setSetting("maxCacheSize", Math.max(1, size));
    }
    
    public boolean isDebugMode() {
        return getBooleanSetting("debugMode", false);
    }
    
    public void setDebugMode(boolean debug) {
        setSetting("debugMode", debug);
    }
    
    public String getLogLevel() {
        return getStringSetting("logLevel", "INFO");
    }
    
    public void setLogLevel(String level) {
        setSetting("logLevel", level);
    }
    
    public int getTarotShuffleIterations() {
        return getIntSetting("tarotShuffleIterations", 7);
    }
    
    public void setTarotShuffleIterations(int iterations) {
        setSetting("tarotShuffleIterations", Math.max(1, iterations));
    }
    
    // 复制配置
    public AlgorithmConfig copy() {
        AlgorithmConfig config = new AlgorithmConfig();
        config.settings = new HashMap<>(this.settings);
        return config;
    }
    
    // 合并配置
    public void merge(AlgorithmConfig other) {
        if (other != null && other.settings != null) {
            this.settings.putAll(other.settings);
        }
    }
    
    // 重置为默认配置
    public void resetToDefaults() {
        settings.clear();
        initializeDefaultSettings();
    }
    
    @Override
    public String toString() {
        return "AlgorithmConfig{" +
                "settings=" + settings.size() + " items" +
                ", secureRandom=" + isSecureRandomEnabled() +
                ", cache=" + isCacheEnabled() +
                ", debug=" + isDebugMode() +
                '}';
    }
}
